import Vue from "vue"
import Vuex from "vuex"
Vue.use(Vuex)

var userdata = uni.getStorageSync('user') ? uni.getStorageSync('user') : ''
console.log(userdata);
const store = new Vuex.Store({
	state: {
		avatar: userdata ? userdata.avatar : '',
		username: userdata ? userdata.username : '',
		account: userdata ? userdata.account : '',
		token: userdata ? userdata.token : '',
		isLogin: userdata ? userdata.isLogin : ''
	},
	getters: {
		getUserName: state => {
			return state.username
		},
		getUserAccount: state => {
			return state.account
		},
		getUserAvatar: state => {
			return state.avatar
		},
		getLogin: state => {
			return state.isLogin
		},
		getToken: state => {
			return state.token
		}
	},
	mutations: {
		setUserName(state, name) {
			state.username = name
		},
		setUserAccount(state, account) {
			state.account = account
		},
		setAvatar(state, avatar) {
			state.avatar = avatar
		},
		setUser(state, data) {
			state.avatar = data.user_avatar
			state.username = data.user_name
			state.account = data.user_account
			state.token = data._id
			state.isLogin = true
			uni.setStorageSync('user', {
				avatar: data.user_avatar,
				username: data.user_name,
				account: data.user_account,
				token: data._id,
				isLogin: true
			})
		},
		logout(state) {
			state.avatar = ""
			state.username = ""
			state.account = ""
			state.token = ""
			state.isLogin = false
			uni.removeStorageSync('user')
		}
	}
})

export default store